package com.ecoolex.chinadutyfree.retailer.admin.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.chinadutyfree.retailer.admin.core.AdminConstant;
import com.ecoolex.chinadutyfree.retailer.entity.Store;
import com.ecoolex.chinadutyfree.retailer.entity.Store.Column;
import com.ecoolex.chinadutyfree.retailer.pojo.AddStoreDTO;
import com.ecoolex.chinadutyfree.retailer.pojo.EditPartStoreDTO;
import com.ecoolex.chinadutyfree.retailer.pojo.EditStoreDTO;
import com.ecoolex.chinadutyfree.retailer.ws.UnionPayStoreService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.service.storage.FileSize;
import com.ecoolex.framework.modularize.api.service.storage.MimeType;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.modularize.api.service.storage.Upload;
import com.ecoolex.framework.modularize.api.service.storage.UploadLimit;
import com.ecoolex.framework.modularize.api.service.storage.UploadSource;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.google.common.collect.Lists;

@GraphQL2Module(type = Type.Admin, description = "商户信息管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = StoreGraphql.AUTH, name = "商户信息管理")
public class StoreGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-STORE";
	
	public static final String AUTH_ADD = AUTH + "-ADD";
	
	public static final String AUTH_EDIT = AUTH + "-EDIT";
	
	public static final String AUTH_EDIT_PART = AUTH + "-EDIT-PART";
	
	public static final String AUTH_GET = AUTH + "-GET";
	
	public static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@Reference
	private UnionPayStoreService unionPayStoreService;
	
	@Autowired
	private StorageService storageService;
	
	@TypeField(type = Store.class,field = "logo",description = "Logo")
	public StorageInfo logo(@Source Store source) {
		return storageService.get(source.getLogo());
	}
	
	@MutationField(field = "uploadLogo", description = { "上传商户Logo" })
	public StorageInfo uploadLogo(@NonNull Upload request, AdminMemberAuthority user) {
		if (!user.auth(AUTH_ADD) && !user.auth(AUTH_EDIT)) {
            user.notAuthority();
        }
        UploadSource uploadSource = new UploadSource("Store", "Logo");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(2));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());

        return storageService.upload(request, uploadSource, uploadLimit);
	}
	
	@QueryField(field = "store",description = "商户信息")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "商户")
	public Store get(@NonNull String storeId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return unionPayStoreService.get(storeId);
	}
	
	@QueryField(field = "storeList",description = "商户信息")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "商户信息")
	public Pagination<Store> query(@NonNull Page page,AdminMemberAuthority user) {
		user.assertAuth(AUTH_QUERY);
		return unionPayStoreService.queryStore(page);
	}
	
	@MutationField(field = "addStore", description = "添加商户")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加商户")
	public Store add(@NonNull AddStoreDTO record,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return unionPayStoreService.addStore(record, user.userName());
	}
	
	@MutationField(field = "editStore", description = "编辑商户")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑商户")
	public Store edit(@NonNull EditStoreDTO record,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		Store old = unionPayStoreService.get(record.getStoreId());
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "商户不存在");
		BeanUtil.copy(record,
			old,
			Lists.newArrayList(Column.deleted, Column.createUser, Column.createTime).stream().map(
				Column::getJavaProperty).toArray(String[]::new));
		return unionPayStoreService.editStore(old, user.userName());
	}
	
	@MutationField(field = "editStorePartInfo", description = "编辑商户部分信息")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT_PART, name = "编辑商户部分信息")
	public Store editPart(@NonNull EditPartStoreDTO record,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT_PART);
		Store old = unionPayStoreService.get(record.getStoreId());
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "商户不存在");
		BeanUtil.copy(record,
			old,
			Lists.newArrayList(
				Column.type,
				Column.externalStoreId,
				Column.serviceId,
				Column.homePageUrl,
				Column.detailUrl,
				Column.deleted,
				Column.createUser, 
				Column.createTime).stream().map(
				Column::getJavaProperty).toArray(String[]::new));
		return unionPayStoreService.editStorePartInfo(old, user.userName());
	}
}
